//
//  ZuZhiViewController.m
//  ZhuoxinAgriculture
//
//  Created by 左忠飞 on 16/11/8.
//  Copyright © 2016年 KKING. All rights reserved.
//

#import "ZuZhiViewController.h"
#import "ZuzhiCell.h"
#import "HeZhuoSheViewController.h"
#import "ZuzhiDetail.h"
#import "Masonry.h"
#import "ZuzhiSearchViewController.h"
@interface ZuZhiViewController () <
UITableViewDelegate,
UITableViewDataSource,
UISearchBarDelegate
>




@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (nonatomic, strong) UITableView *tbView;
@property (nonatomic, strong) UISearchBar *search;
@property (nonatomic, strong) UIView *sideView;
@property (nonatomic, strong) UIButton *saixuan;
@property (nonatomic, weak) UIView *navView;
@property (nonatomic, strong) UIWindow *mainWindow;
@property (nonatomic, strong) UIButton *coverBtn;



//数据
@property (nonatomic, strong) NSArray *cellData;

@end

@implementation ZuZhiViewController
-(void)dealloc{
    [self.coverBtn removeFromSuperview];
    [self.sideView removeFromSuperview];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.mainWindow = [UIApplication sharedApplication].keyWindow;
    self.tabBarController.tabBar.hidden = YES;
    self.title = @"组织列表";
    
    [self setupUI];
}

- (void)setupUI
{
    
    self.search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 64, kScreenW, 44)];
    self.search.placeholder = @"搜索农场/大户/合作社";
    self.search.showsCancelButton = YES;
    self.search.delegate = self;
    [self.view addSubview:self.search];
    for (id cencelButton in [self.search.subviews[0] subviews])
    {
        if([cencelButton isKindOfClass:[UIButton class]])
        {
            UIButton *btn = (UIButton *)cencelButton;
            [btn setTitle:@"筛选"  forState:UIControlStateNormal];
            btn.enabled = YES;
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    self.navView = self.navigationController.view;
    [self.view addSubview:self.tbView];

    [self.view addSubview:self.saixuan];
    [self.saixuan mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(kI(100));
        make.height.mas_equalTo(kI(88.0));
        make.right.mas_equalTo(self.view.mas_right);
        make.top.mas_equalTo(self.view).offset(64);
    }];
    
    //add cover
    [self.navView addSubview:self.coverBtn];
    [self.coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.left.right.mas_equalTo(self.navView);
    }];
    self.coverBtn.hidden = YES;
    
    //设置 sideView
    [self.navView addSubview:self.sideView];
    
    [self.sideView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.navView.mas_right);
        make.width.mas_equalTo(kI(676.0));
        make.height.mas_equalTo(kScreenH);
        make.top.mas_equalTo(self.navView);
    }];
    [self setupSideView];
    
    
    
}

- (void)setupSideView
{

    UILabel *leixing = [UILabel new];
    leixing.text = @"类型";
    leixing.font = [UIFont systemFontOfSize:kI(24.0)];
    leixing.textColor = UIColorFromRGBA(0x000000);
    [leixing sizeToFit];
    [self.sideView addSubview:leixing];
    
    [leixing mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.sideView).offset(kI(44.0));
        make.top.mas_equalTo(self.sideView.mas_top).offset(kI(108.0));
    }];
    
    UIView *btnWrapperView = [UIView new];
    [self.sideView addSubview:btnWrapperView];
    [btnWrapperView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(leixing.mas_bottom).mas_offset(kI(24.0));
        make.leading.mas_equalTo(leixing);
        make.right.mas_equalTo(self.sideView.mas_right).offset(-kI(44.0));
        make.height.mas_equalTo(kI(50.0));
    }];
    
    UIButton *dahuBtn = [self btnWithTitle:@"大户" withSEL:@selector(dahuAction)];
    UIButton *nongchangBtn = [self btnWithTitle:@"农场" withSEL:@selector(nongchangAction)];
    UIButton *xiaonongBtn = [self btnWithTitle:@"小农" withSEL:@selector(xiaonongAction)];
    NSArray *arrBtn = @[dahuBtn, nongchangBtn, xiaonongBtn];
    for (UIButton *btn in arrBtn) {
        [btnWrapperView addSubview:btn];
    }
    [arrBtn mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:kI(16.0) leadSpacing:kI(0.0) tailSpacing:kI(0.0)];
    [arrBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(btnWrapperView);
    }];
    
    UIView *sepView = [UIView new];
    sepView.backgroundColor = UIColorFromRGBA(0xe2e2e2);
    [self.sideView addSubview:sepView];
    [sepView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(btnWrapperView.mas_bottom).offset(kI(24.0));
        make.left.mas_equalTo(self.sideView);
        make.width.mas_equalTo(self.sideView);
        make.height.mas_equalTo(1);
    }];
    
    UIView *btmView = [UIView new];
    btmView.backgroundColor = UIColorFromRGBA(0xf5f5f5);
    [self.sideView addSubview:btmView];
    [btmView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.sideView);
        make.height.mas_equalTo(kI(90));
        make.bottom.mas_equalTo(self.sideView);
        make.left.mas_equalTo(self.sideView);
    }];
    
    UIButton *wancheng = [self btmBtnWith:@"完成" txtColor:UIColor.whiteColor bgColor:UIColorFromRGBA(0x67b245)];
    [wancheng addTarget:self action:@selector(wanchengClick) forControlEvents:UIControlEventTouchUpInside];
    UIButton *chongzhi = [self btmBtnWith:@"重置" txtColor: UIColorFromRGBA(0x67b245) bgColor:UIColorFromRGBA(0xe0ecda)];
    [btmView addSubview:wancheng];
    [btmView addSubview:chongzhi];
    [wancheng mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(btmView);
        make.height.mas_equalTo(btmView);
        make.top.mas_equalTo(btmView);
        make.width.mas_equalTo(kI(180.0));
    }];
    
    [chongzhi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(wancheng.mas_left);
        make.width.height.mas_equalTo(wancheng);
        make.top.mas_equalTo(wancheng);
    }];
    
    
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    for (id cencelButton in [self.searchBar.subviews[0] subviews])
    {
        if([cencelButton isKindOfClass:[UIButton class]])
        {
            UIButton *btn = (UIButton *)cencelButton;
            btn.enabled = YES;
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }

}

#pragma mark - BTN METHOD
- (void)saixuanClick
{
    CGFloat alpha;
    self.coverBtn.hidden = !self.coverBtn.hidden;
    //add cover
    if (self.coverBtn.hidden) {
        alpha = 0.0;
        
    } else {
        alpha = 0.7;
    }
    
    
    if (!self.coverBtn.hidden) {
        [self.sideView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.navView.mas_left).offset(kI(74));
            make.width.mas_equalTo(kI(676.0));
            make.height.mas_equalTo(kScreenH);
            make.top.mas_equalTo(self.navView);
        }];
    } else {
        [self.sideView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.navView.mas_right);
            make.width.mas_equalTo(kI(676.0));
            make.height.mas_equalTo(kScreenH);
            make.top.mas_equalTo(self.navView);
        }];
    }
    [UIView animateWithDuration:0.25 animations:^{
        self.coverBtn.alpha = alpha;
        [self.navView layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)coverBtnClick
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.coverBtn.hidden = YES;
    });
    [self saixuanClick];
    
}

- (void)wanchengClick
{
    [self coverBtnClick];
}

#pragma mark - searchBar delegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    ZuzhiSearchViewController *vc = [ZuzhiSearchViewController new];
    vc.view.backgroundColor = UIColor.whiteColor;
    [self.navigationController pushViewController:vc animated:NO];
    return NO;
}

#pragma mark -tableViewDatasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cellData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZuzhiCell *cell = [ZuzhiCell zuzhiCellWithTableView:tableView];
    
    cell.cellData = self.cellData[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (UITableView *)tbView
{
    if (!_tbView) {
        CGFloat tbY = CGRectGetMaxY(self.search.frame);
        _tbView = [[UITableView alloc] initWithFrame:CGRectMake(0, tbY, kScreenW, kScreenH-tbY) style:UITableViewStylePlain];
        _tbView.delegate = self;
        _tbView.dataSource = self;
        _tbView.rowHeight = 112;
        _tbView.tableFooterView = [UIView new];
        _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tbView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        //农场
        ZuzhiDetail *zuzhi = [[ZuzhiDetail alloc] init];
        zuzhi.number = @"农场";
        [self.navigationController pushViewController:zuzhi animated:YES];
    } else if (indexPath.row == 1) {
        ZuzhiDetail *zuzhi = [[ZuzhiDetail alloc] init];
        zuzhi.number = @"农场";
        [self.navigationController pushViewController:zuzhi animated:YES];
    } else if (indexPath.row == 2) {
        ZuzhiDetail *zuzhi = [[ZuzhiDetail alloc] init];
        zuzhi.number = @"大户";
        [self.navigationController pushViewController:zuzhi animated:YES];
    
    }else if (indexPath.row == 3) {
        //
        HeZhuoSheViewController *sylbVC = [HeZhuoSheViewController new];
        [self.navigationController pushViewController:sylbVC animated:YES];
    }
}

#pragma mark searchBar delegate


#pragma mark 懒加载
-(NSArray *)cellData {
    if (!_cellData) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"ZuzhiVC.plist" ofType:nil];
        _cellData = [NSArray arrayWithContentsOfFile:path];
    }
    return _cellData;
}

- (UIView *)sideView {
    if (!_sideView) {
        _sideView = [[UIView alloc] init];
        _sideView.backgroundColor = [UIColor whiteColor];
    }
    return _sideView;
}

- (UIButton *)saixuan
{
    if (!_saixuan) {
        _saixuan = [UIButton new];
        [_saixuan addTarget:self action:@selector(saixuanClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saixuan;
}

- (UIButton *)coverBtn
{
    if (!_coverBtn) {
        _coverBtn = [UIButton new];
        _coverBtn.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.75];
        [_coverBtn addTarget:self action:@selector(coverBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _coverBtn;
}

- (UIButton *)btnWithTitle:(NSString *)title withSEL:(SEL)selector
{
    UIButton *btn = [UIButton new];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:kI(24.0)];
    [btn setTitleColor:UIColorFromRGBA(0x000000) forState:UIControlStateNormal];
    [btn addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
    [btn setBackgroundColor:UIColorFromRGBA(0xf5f5f5)];
    return btn;
}

- (UIButton *)btmBtnWith:(NSString *)title txtColor:(UIColor *)txtcolor bgColor:(UIColor *)bgColor
{
    UIButton *btn = [UIButton new];
    btn.backgroundColor = bgColor;
    [btn setTitleColor:txtcolor forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:kI(24.0)];
    return btn;
}
- (void)dahuAction
{
    NSLog(@"大户");
}

- (void)nongchangAction
{
    NSLog(@"农场");
}
- (void)xiaonongAction
{
    NSLog(@"小农");
}
- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.alpha = 1.0;
}


@end
